<template>
    <div class="menu-lists">
        <el-card class="!border-none" shadow="never">
            <el-card class="!border-none" shadow="never">
                <el-form ref="formRef" class="mb-[-16px]" :model="queryParams" :inline="true">
                    <el-form-item label="分类名称">
                        <el-input
                            class="w-[280px]"
                            v-model="queryParams.name"
                            clearable
                            @keyup.enter="getLists()"
                        />
                    </el-form-item>
                    <el-form-item>
                        <el-button type="primary" @click="getLists()">查询</el-button>
                    </el-form-item>
                </el-form>
            </el-card>
            <div>
                <el-button type="primary" @click="handleAdd()">
                    <template #icon>
                        <icon name="el-icon-Plus" />
                    </template>
                    新增
                </el-button>
                <el-button @click="handleExpand"> 展开/折叠 </el-button>
            </div>
            <el-table
                v-loading="loading"
                ref="tableRef"
                class="mt-4"
                size="large"
                :data="lists"
                row-key="id"
                :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
            >
                <el-table-column label="名称" prop="name" min-width="150" show-overflow-tooltip />
                <el-table-column label="排序" prop="sort" min-width="100" />
                <el-table-column label="图标" width="120" fixed="right">
                    <template #default="{ row }">
                        <el-image
                            preview
                            style="width: 20px; height: 20px"
                            :src="row.icon"
                            :fit="row.icon"
                        />
                    </template>
                </el-table-column>
                <el-table-column label="操作" width="160" fixed="right">
                    <template #default="{ row }">
                        <el-button
                            v-if="row.menuType !== MenuEnum.BUTTON"
                            type="primary"
                            link
                            @click="handleAdd(row.id)"
                        >
                            新增
                        </el-button>
                        <el-button type="primary" link @click="handleEdit(row)"> 编辑 </el-button>
                        <el-button type="danger" link @click="handleDelete(row.id)">
                            删除
                        </el-button>
                    </template>
                </el-table-column>
            </el-table>
        </el-card>
        <edit-popup v-if="showEdit" ref="editRef" @success="getLists" @close="showEdit = false" />
    </div>
</template>
<script lang="ts" setup name="category">
import type { ElTable } from 'element-plus'
import { MenuEnum } from '@/enums/appEnums'
import EditPopup from './edit.vue'
import feedback from '@/utils/feedback'
import {
    listTree as menuLists,
    del as menuDelete
} from '@/servers/goods/api/supplyChainshangpinfenlei'
const tableRef = shallowRef<InstanceType<typeof ElTable>>()
const editRef = shallowRef<InstanceType<typeof EditPopup>>()
let isExpand = false
const loading = ref(false)
const showEdit = ref(false)
const lists = ref([])

const queryParams = ref({
    name: undefined
})
const getLists = async () => {
    loading.value = true
    try {
        const data = await menuLists({
            ...queryParams.value
        })
        lists.value = data
        loading.value = false
    } catch (error) {
        loading.value = false
    }
}

const resetParams = () => {
    queryParams.value = {
        name: undefined
    }
}

const handleAdd = async (id?: number) => {
    showEdit.value = true
    await nextTick()
    if (id) {
        editRef.value?.setFormData({
            pid: id
        })
    }
    editRef.value?.open('add')
}

const handleEdit = async (data: any) => {
    showEdit.value = true
    await nextTick()
    editRef.value?.open('edit')
    editRef.value?.getDetail(data)
}

const handleDelete = async (id: number) => {
    await feedback.confirm('确定要删除？')
    await menuDelete({ id })
    feedback.msgSuccess('删除成功')
    getLists()
}

const handleExpand = () => {
    isExpand = !isExpand
    toggleExpand(lists.value, isExpand)
}

const toggleExpand = (children: any[], unfold = true) => {
    for (const key in children) {
        tableRef.value?.toggleRowExpansion(children[key], unfold)
        if (children[key].children) {
            toggleExpand(children[key].children!, unfold)
        }
    }
}

getLists()
</script>
